﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DistributeTransaction.DAL;
using DistributeTransaction.Models;
using DistributeTransaction.Utils;
using log4net;

namespace DistributeTransaction.Controllers
{
    public class InformationController : BaseController
    {
        private ILog log = LogManager.GetLogger(typeof(InformationController));
        private CardRepository cardRepository;

        public InformationController()
            : base()
        {
            cardRepository = new CardRepository(DBContext); 
        }

        //
        // GET: /Information/

        public ActionResult Index()
        {
            return View(cardRepository.GetCardInfos(CustomerId));
        }

        [HttpGet]
        [ActionName("Delete")]
        public ActionResult DeleteConfirm(int Id)
        {
            return View(cardRepository.FindCardById(Id));
        }

        [HttpPost]
        [ActionName("Delete")]
        public ActionResult Delete(int Id)
        {
            cardRepository.DeleteCardById(Id);
            return RedirectToAction("index", "information");
        }

        [HttpGet]
        [ActionName("Create")]
        public ActionResult CreateNew()
        {
            return View(new CardInfo());
        }

        [HttpPost]
        [ActionName("Create")]
        public ActionResult CreateNew(CardInfo cardInfo)
        {
            if (cardInfo != null)
            {
                cardRepository.CreatNewItem(cardInfo, CustomerId);
            }
            return RedirectToAction("index", "information");
        }

        [HttpGet]
        [ActionName("Edit")]
        public ActionResult Edit(int Id)
        {
            return View(cardRepository.FindCardById(Id));
        }

        [HttpPost]
        [ActionName("Edit")]
        public ActionResult Edit(CardInfo cardInfo)
        {
            cardRepository.SaveCardInfo(cardInfo);
            return RedirectToAction("index", "information");
        }

        [HttpPost]
        [ValidateInput(false)]
        public JsonResult SaveCard(string holdName, string cardNumber, string cardType, string month, string year, string validCode)
        {
            CardInfo cardInfo = new CardInfo();
            cardInfo.HoldName = holdName;
            cardInfo.CardNumber = cardNumber;
            cardInfo.CardType = cardType;
            cardInfo.ExpireMonth = month;
            cardInfo.ExpireYear = year;
            cardInfo.ValidCode = validCode;

            cardRepository.CreatNewItem(cardInfo, CustomerId);

            return Json(new
            {
                Message = cardInfo.AccountId
            }, JsonRequestBehavior.AllowGet);
        }

    }

}
